//
//  UIImage+Compression.m
//  PhotoLibrary&HandleImage
//
//  Created by ya on 16/11/23.
//  Copyright © 2016年 ya. All rights reserved.
//

#import "UIImage+Compression.h"

@implementation UIImage (Compression)
- (UIImage *)sj_categryCompressionImage {

    CGFloat selfW = self.size.width;
    CGFloat selfH = self.size.height;
    CGFloat screenW = [[UIScreen mainScreen] bounds].size.width;
    CGFloat screenH = [[UIScreen mainScreen] bounds].size.height;

    if (selfW >= selfH)
    {
        if (screenW >= selfW)
        {
            return self;
        }
        CGFloat maxH = screenW * selfH / selfW;

        return [self drawImageWithSize:CGSizeMake(screenW, maxH)];
    }
    else
    {
        if (screenH >= selfH)
        {
            return self;
        }
        CGFloat maxW = selfW * screenH / selfH;

        return [self drawImageWithSize:CGSizeMake(maxW, screenH)];
    }
}

- (UIImage *)drawImageWithSize:(CGSize)size {
    UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *compressionImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return compressionImage;
}
@end
